//
//  ZHToolBarView.swift
//  ZHWeibo
//
//  Created by user on 15/12/10.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

class ZHToolBarView: UIView {
    
    var retweetButton: UIButton?
    var commentButton: UIButton?
    var unlikeButton: UIButton?
    var leftSP:UIImageView?
    var rightSP:UIImageView?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }
    //设置模型,重写模型的didSet方法
    var statusUserViewModel:ZHStatusUserViewModel? {
        didSet{
            //设置三个button的文字
            retweetButton?.setTitle(statusUserViewModel?.retweetCountStr, forState: .Normal)
            commentButton?.setTitle(statusUserViewModel?.commentCountStr, forState: .Normal)
            unlikeButton?.setTitle(statusUserViewModel?.attitudeCountStr, forState: .Normal)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setUpUI(){
        //创建控件
        retweetButton = getButtonWith(title: "转发", imageName: "timeline_icon_retweet")
        commentButton = getButtonWith(title: "评论", imageName: "timeline_icon_comment")
        unlikeButton = getButtonWith(title: "赞", imageName: "timeline_icon_unlike")
        leftSP = getSeparator()
        rightSP = getSeparator()
        //添加约束
        retweetButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton?.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(unlikeButton!)
        }
        
        unlikeButton?.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
        }
        
        leftSP?.snp_makeConstraints{ (make) -> Void in
            /// 使用这个约束的特点,tralling不代表数值
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        rightSP?.snp_makeConstraints{ (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
    }
    /// 为什么这种方式不能够实现懒加载,只有系统的方式才可以实现懒加载
    //lazy var retweetBtn:UIButton = addChildButton("ccc", imgName: "eeeee")
}

extension ZHToolBarView {
    //初始化子Button的方法
    private func getButtonWith(title title: String, imageName: String ) -> UIButton {
        let button = UIButton()
        // 设置文字,字体大小,不同状态的字体颜色
        button.setTitle(title, forState: .Normal)
        button.setTitleColor(UIColor.grayColor(), forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Highlighted)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        // 设置图标
        button.setImage(UIImage(named: imageName), forState: .Normal)
        // 设置不同状态下的背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        addSubview(button)
        return button
    }
    
    private func getSeparator() -> UIImageView{
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
    
}













